package com.hxd.mall.widget

import android.content.Context
import android.text.Editable
import android.text.InputFilter
import android.text.InputType
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.hxd.mall.R
import com.hxd.mall.base.IBaseWidgetView
import com.hxd.mall.utils.open.addSetOnClickListener
import com.hxd.mall.utils.open.addTextWatcherListener


/**
 * 登录注册输组件(input: 0 登录界面手机号输入框，1 登录界面密码输入框,2 注册界面设置密码输入框,3 忘记密码界面输入密码框,4 忘记密码界面 手机号输入框)
 * Created by qishuai on 2018/7/23.
 */
class HxdLoginAndPwdView(context: Context?, var attrs: AttributeSet?) :
    LinearLayout(context, attrs),
    IBaseWidgetView {
    // input type
    private var mInputType = 0
    // rootView
    private lateinit var mLayoutInputRoot: View
    // title name tv
    private lateinit var mTvTitleNameTip: TextView
    // title name desc tv
    private lateinit var mTvTitleNameDescTip: TextView
    // input error tip tv
    private lateinit var mTvErrorTip: TextView
    // 输入框 左边图标
    private lateinit var mIvInputLeftIconTip: ImageView
    // 输入框
    private lateinit var mEvInput: EditText
    // 输入框 右边清空图标
    private lateinit var mIvInputRightClearIconTip: ImageView
    // 输入框 右边密码隐藏图标
    private lateinit var mIvInputRightHiddenPwdIconTip: ImageView
    // 二次确认框
    private lateinit var mLayoutInputAgain: View
    // 输入框 左边图标
    private lateinit var mIvInputLeftIconTipAgain: ImageView
    // 输入框
    private lateinit var mEvInputAgain: EditText
    // 输入框 右边清空图标
    private lateinit var mIvInputRightClearIconTipAgain: ImageView
    // 输入框 右边密码隐藏图标
    private lateinit var mIvInputRightHiddenPwdIconTipAgain: ImageView
    // next btn
    private lateinit var mBtnNext: Button
    // next tip tv
    private lateinit var mTvNextTip: TextView
    // 是否可以看到密码明文
    private var mSeePwdState = false
    private var mSeePwdStateAgain = false

    init {
        setupView()
        setupData()
        setupLayout()
        setupGesture()
    }

    /**
     * ===============Init=============
     */
    override fun setupView() {
        LayoutInflater.from(context).inflate(R.layout.widget_login_input_phone_pwd, this)
        mTvTitleNameTip = this.findViewById(R.id.tv_title_name_tip)
        mTvTitleNameDescTip = this.findViewById(R.id.tv_title_name_desc_tip)
        mTvErrorTip = this.findViewById(R.id.tv_error_tip)
        mIvInputLeftIconTip = this.findViewById(R.id.iv_phone_icon_tip)
        mEvInput = this.findViewById(R.id.et_phone_input)
        mIvInputRightClearIconTip = this.findViewById(R.id.iv_phone_input_text_clear_icon)
        mIvInputRightHiddenPwdIconTip = this.findViewById(R.id.iv_pwd_input_text_see_icon)
        mIvInputLeftIconTipAgain = this.findViewById(R.id.iv_phone_icon_tip_again)
        mLayoutInputAgain = this.findViewById(R.id.layout_input_ev_again)
        mEvInputAgain = this.findViewById(R.id.et_phone_input_again)
        mIvInputRightClearIconTipAgain = this.findViewById(R.id.iv_phone_input_text_clear_icon_again)
        mIvInputRightHiddenPwdIconTipAgain = this.findViewById(R.id.iv_pwd_input_text_see_icon_again)
        mBtnNext = this.findViewById(R.id.btn_next)
        mTvNextTip = this.findViewById(R.id.tv_next_tip)
        mLayoutInputRoot = this.findViewById(R.id.layout_input_ev)
    }

    override fun setupData() {
        // 获取文本框类型
        val typeArray = context?.obtainStyledAttributes(attrs, R.styleable.HxdLoginAndPwdView)
        mInputType = typeArray?.getInt(R.styleable.HxdLoginAndPwdView_et_type, -1) ?: 0
        typeArray?.recycle()
    }

    override fun setupLayout() {
        when (mInputType) {
            0 -> {
                // 登录手机号输入界面初始化
                initViewLoginInputPhone()
                //mEvInput.tag = "-100"
                //mBtnNext.tag = "-101"
            }
            1 -> {
                // 登录界面密码输入框
                initViewLoginInputPwd()
            }
            2 -> {
                // 注册界面设置密码输入框
                initViewRegisterSetPwd()
                // mEvInput.tag = "-104"
                //mBtnNext.tag = "-105"
            }
            3 -> {
                // 忘记密码界面 密码输入框
                initViewForgetPwdSetPwd()
                //mEvInput.tag = "-108"
            }
            4 -> {
                // 忘记密码界面 手机号输入框
                initViewForgetPwdInputPhone()
                //mEvInput.tag = "-106"
            }
            5 -> {
                //重置密码 密码输入框
                initViewResetPwd()
            }
            6 ->
                //更换手机号-新手机号输入框
                initViewChangePhoneInputPhone()
        }
    }

    override fun setupGesture() {

    }

    /**
     * ===============Private.初始化方法=============
     */
    /**
     * 忘记密码界面输入密码框
     */
    private fun initViewForgetPwdSetPwd() {
        mTvTitleNameTip.text = context.getString(R.string.login_input_forget_pwd_title_tip)
        mBtnNext.text = context.getString(R.string.login_name_go)
        mTvTitleNameDescTip.text = context.getString(R.string.login_input_pwd_desc_tip)

        initPwdCommon()
    }

    /**
     * 重置密码界面输入密码框
     */
    private fun initViewResetPwd() {
        mTvTitleNameTip.text = context.getString(R.string.account_update_pwd_title_tip)
        mTvTitleNameDescTip.text = context.getString(R.string.login_input_pwd_desc_tip)
        mBtnNext.text = context.getString(R.string.borrow_form_commit_btn_name)
        mLayoutInputAgain.visibility = View.VISIBLE

        initPwdCommon()
        initPwdAgainCommon()
    }

    /**
     * 登录手机号输入界面初始化
     */
    private fun initViewLoginInputPhone() {
        mTvTitleNameTip.text = context.getString(R.string.login_input_phone_tip)
        mTvTitleNameDescTip.text = context.getString(R.string.login_input_phone_tip_desc)
        mTvNextTip.text = context.getString(R.string.login_btn_next_tip)
        initPhoneCommon()
    }

    /**
     * 忘记密码界面 手机号输入框
     */
    private fun initViewForgetPwdInputPhone() {
        mTvTitleNameTip.text = context.getString(R.string.login_input_phone_tip)
        mTvTitleNameDescTip.text = context.getString(R.string.forget_input_phone_tip_desc)
        mTvNextTip.visibility = View.GONE
        initPhoneCommon()
    }

    /**
     * 更换手机号-新手机号输入框
     */
    private fun initViewChangePhoneInputPhone() {
        mTvTitleNameTip.text = context.getString(R.string.account_change_phone_title)
        mTvTitleNameDescTip.text = context.getString(R.string.account_change_phone_title_tip)
        mTvNextTip.visibility = View.GONE
        initPhoneCommon()
    }

    private fun initPhoneCommon() {
        mTvErrorTip.text = ""
        mIvInputLeftIconTip.setImageResource(R.mipmap.login_input_phone_tip)
        mEvInput.run {
            hint = context.getString(R.string.login_input_phone_tip)
            filters = arrayOf<InputFilter>(InputFilter.LengthFilter(11))
            inputType = InputType.TYPE_CLASS_PHONE
        }
        mIvInputRightClearIconTip.visibility = View.INVISIBLE
        mIvInputRightHiddenPwdIconTip.visibility = View.GONE
        mBtnNext.run {
            isEnabled = false
            setBackgroundResource(R.mipmap.btn_disabled)
            text = context.getString(R.string.login_btn_next)
        }

        addTextWatcherListener(mEvInput) {
            // 验证手机号11位
            if (it.length == 11) {
                mBtnNext.isEnabled = true
                mBtnNext.setBackgroundResource(R.mipmap.btn_normal)
            } else {
                mBtnNext.isEnabled = false
                mBtnNext.setBackgroundResource(R.mipmap.btn_disabled)
            }
            // 显示清除Icon
            if (it.isEmpty()) {
                mIvInputRightClearIconTip.visibility = View.INVISIBLE
            } else {
                mIvInputRightClearIconTip.visibility = View.VISIBLE
            }
        }

        mIvInputRightClearIconTip.setOnClickListener {
            mEvInput.text.clear()
        }
    }

    /**
     * 登录界面密码输入框
     */
    private fun initViewLoginInputPwd() {
        mTvTitleNameTip.text = context.getString(R.string.login_input_pwd_title_tip)
        mBtnNext.text = context.getString(R.string.login_name)
        mTvTitleNameDescTip.text = context.getString(R.string.login_input_pwd_desc_tip)
        initPwdCommon()
    }

    /**
     * 初始化输入密码类型布局(设置密码)
     */
    private fun initViewRegisterSetPwd() {
        mTvTitleNameTip.text = context.getString(R.string.login_input_set_pwd_title_tip)
        mBtnNext.text = context.getString(R.string.register_name)
        mTvTitleNameDescTip.text = context.getString(R.string.login_input_pwd_desc_tip)
        initPwdCommon()
    }

    private fun initPwdCommon() {
        mTvErrorTip.text = ""
        mIvInputLeftIconTip.setImageResource(R.mipmap.login_pwd_tip_icon)
        mEvInput.run {
            hint = if (mInputType == 5) {
                context.getString(R.string.account_update_pwd_input_old_tip)
            } else {
                context.getString(R.string.login_input_pwd_tip)
            }
            filters = arrayOf<InputFilter>(InputFilter.LengthFilter(18))
            inputType = InputType.TYPE_CLASS_TEXT
            transformationMethod = PasswordTransformationMethod.getInstance()
        }
        mIvInputRightHiddenPwdIconTip.run {
            visibility = View.VISIBLE
            setImageResource(R.mipmap.pwd_input_no_see_icon)
        }
        mBtnNext.run {
            isEnabled = false
            setBackgroundResource(R.mipmap.btn_disabled)
        }
        mIvInputRightClearIconTip.visibility = View.INVISIBLE
        mTvNextTip.text = ""

        mIvInputRightHiddenPwdIconTip.setOnClickListener {
            if (mSeePwdState) {
                mSeePwdState = false
                mEvInput.transformationMethod = PasswordTransformationMethod.getInstance()
                mIvInputRightHiddenPwdIconTip.setImageResource(R.mipmap.pwd_input_no_see_icon)
            } else {
                mSeePwdState = true
                mEvInput.transformationMethod = HideReturnsTransformationMethod.getInstance()
                mIvInputRightHiddenPwdIconTip.setImageResource(R.mipmap.pwd_input_can_see_icon)
            }
            mEvInput.apply { setSelection(text.length) }
        }

        addTextWatcherListener(mEvInput) {
            val length = mEvInput.text.length
            val lengthAgain = mEvInputAgain.text.toString().length
            // 修改密码特殊判断
            if (mInputType == 5) {
                // 验证手机号11位
                pwdTextListenerCommon(length in 8..18 && lengthAgain in 8..18)
            } else {
                // 验证手机号11位
                pwdTextListenerCommon(length in 8..18)
            }
            // 显示清除Icon
            if (it.isEmpty()) {
                mIvInputRightClearIconTip.visibility = View.INVISIBLE
            } else {
                mIvInputRightClearIconTip.visibility = View.VISIBLE
            }
        }

        mIvInputRightClearIconTip.setOnClickListener {
            mEvInput.text.clear()
        }
    }

    private fun pwdTextListenerCommon(result: Boolean) {
        if (result) {
            mBtnNext.isEnabled = true
            mBtnNext.setBackgroundResource(R.mipmap.btn_normal)
            setErrorTip("")
        } else {
            mBtnNext.isEnabled = false
            mBtnNext.setBackgroundResource(R.mipmap.btn_disabled)
        }
    }

    private fun initPwdAgainCommon() {
        mTvErrorTip.text = ""
        mIvInputLeftIconTipAgain.setImageResource(R.mipmap.login_pwd_tip_icon)
        mEvInputAgain.run {
            hint = context.getString(R.string.account_update_pwd_input_new_tip)
            filters = arrayOf<InputFilter>(InputFilter.LengthFilter(18))
            inputType = InputType.TYPE_CLASS_TEXT
            transformationMethod = PasswordTransformationMethod.getInstance()
        }
        mIvInputRightHiddenPwdIconTipAgain.run {
            visibility = View.VISIBLE
            setImageResource(R.mipmap.pwd_input_no_see_icon)
        }
        mBtnNext.run {
            isEnabled = false
            setBackgroundResource(R.mipmap.btn_disabled)
        }
        mTvNextTip.text = ""
        mIvInputRightClearIconTipAgain.visibility = View.INVISIBLE
        addSetOnClickListener(mIvInputRightHiddenPwdIconTipAgain) {
            if (mSeePwdStateAgain) {
                mSeePwdStateAgain = false
                mEvInputAgain.transformationMethod = PasswordTransformationMethod.getInstance()
                mIvInputRightHiddenPwdIconTipAgain.setImageResource(R.mipmap.pwd_input_no_see_icon)
            } else {
                mSeePwdStateAgain = true
                mEvInputAgain.transformationMethod = HideReturnsTransformationMethod.getInstance()
                mIvInputRightHiddenPwdIconTipAgain.setImageResource(R.mipmap.pwd_input_can_see_icon)
            }
            mEvInputAgain.apply { setSelection(text.length) }
        }

        addTextWatcherListener(mEvInputAgain) {
            val length = mEvInput.text.length
            val lengthAgain = mEvInputAgain.text.toString().length

            // 修改密码特殊判断
            if (mInputType == 5) {
                // 验证手机号11位
                pwdTextListenerCommon(length in 8..18 && lengthAgain in 8..18)
            } else {
                // 验证手机号11位
                pwdTextListenerCommon(length in 8..18)
            }

            // 显示清除Icon
            if (it.isEmpty()) {
                mIvInputRightClearIconTipAgain.visibility = View.INVISIBLE
            } else {
                mIvInputRightClearIconTipAgain.visibility = View.VISIBLE
            }

        }
        mIvInputRightClearIconTipAgain.setOnClickListener {
            mEvInputAgain.text.clear()
        }
    }

    /**
     * ===============Public.共有方法模块说明=============
     */

    /**
     * 错误提示,外部调用
     */
    fun setErrorTip(message: String) {
        mTvErrorTip.text = message
    }

    /**
     * 单击事件
     */
    fun setNextBtnClickedListener(listener: View.OnClickListener) {
        mBtnNext.setOnClickListener(listener)
    }

    /**
     * 单击事件升级
     */
    fun setNextBtnClickedListener(callback: () -> Unit) {
        addSetOnClickListener(mBtnNext) {
            callback()

        }
    }

    /**
     *
     * 获取输入框内容
     */
    fun getInputTextString(): String {
        return mEvInput.text.toString()
    }

    /**
     * 获取输入框内容Again
     */
    fun getInputTextAgainString(): String {
        return mEvInputAgain.text.toString()
    }

    /**
     * 设置输入框内容
     */
    fun setInputTextString(message: String) {
        mEvInput.text = Editable.Factory.getInstance().newEditable(message)
        mEvInput.setSelection(message.length)
    }

    /**
     * 清空输入框内容
     */
    fun clearInputText() {
        return mEvInput.text.clear()
    }

    /**
     * 获取button按钮
     */
    fun getButtonNext(): Button {
        return mBtnNext
    }

    /**
     * 输入错误背景色变红
     */
    fun setEtErrorTip(backgroundId: Int) {
        mLayoutInputRoot.setBackgroundResource(backgroundId)
    }
}